package com.anyisn.anyflickr.shell.view
{
	import com.adobe.webapis.flickr.PagedPhotoList;
	import com.anyisn.anyflickr.shell.ShellFacade;
	import com.anyisn.anyflickr.shell.model.ListPhotosProxy;
	import com.anyisn.anyflickr.shell.model.vo.PhotoListVO;
	import com.anyisn.anyflickr.shell.model.vo.SearchVO;
	import com.anyisn.anyflickr.shell.view.components.PreviousAndNextHB;
	import com.anyisn.anyflickr.shell.view.components.ThumbsCB;
	import com.anyisn.puremvc.as3.multicore.patterns.mediator.AbstractPopupMediator;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class ThumbsCBMediator extends AbstractPopupMediator
	{
		public static const NAME:String = 'ThumbsCBMediator';
		
		public function ThumbsCBMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
			
			cb.addEventListener(ThumbsCB.PAGE_CHANGE, onPageChange);
			cb.addEventListener(ThumbsCB.PREVIOUS_LIST, getPreviousList);
			cb.addEventListener(ThumbsCB.SLIDESHOW, beginSlide);
			cb.addEventListener(ThumbsCB.BATCH_DOWNLOAD, batchDownload);
		}
		
		override public function listNotificationInterests():Array {
			return [ShellFacade.TILE_PHOTOS_SEARCH,
					ShellFacade.TILE_INTERESTINGNESS_GET_LIST_SUCCESS,
					ShellFacade.TILE_FAVORITES_GET_LIST_SUCCESS,
					ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS,
					ShellFacade.TILE_PHOTOS_SEARCH_SUCCESS
					];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case ShellFacade.TILE_PHOTOS_SEARCH:
					var searchVO:SearchVO = notification.getBody() as SearchVO;
					break;
				case ShellFacade.TILE_INTERESTINGNESS_GET_LIST_SUCCESS:
				case ShellFacade.TILE_FAVORITES_GET_LIST_SUCCESS:
				case ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS:
				case ShellFacade.TILE_PHOTOS_SEARCH_SUCCESS:
					var list:PagedPhotoList = notification.getBody() as PagedPhotoList;
					pagePN.num = list.page;
					pagePN.max = list.pages;
					cb.currentState = 'free';
					break;
			}
		}
		
		private function getPreviousList(e:Event):void {
			sendNotification(ShellFacade.THUMBS_GET_PREVIOUS_PAGE);
		}

		private function onPageChange(e:Event):void {
			sendNotification(ShellFacade.THUMBS_CHANGE_PAGE, pagePN.num);
		}
		
		private function beginSlide(e:Event):void {
			var lp:ListPhotosProxy = facade.retrieveProxy(ListPhotosProxy.NAME) as ListPhotosProxy;
			var cl:PhotoListVO = lp.currentList;
			if(cl) {
				switch(cl.methodName) {
					case ShellFacade.TILE_INTERESTINGNESS_GET_LIST:
						sendNotification(ShellFacade.SLIDE_INTERESTINGNESS_GET_LIST_SUCCESS, cl);
						break;
					case ShellFacade.TILE_PHOTOS_SEARCH:
						sendNotification(ShellFacade.SLIDE_PHOTOS_SEARCH_SUCCESS, cl);
						break;
					case ShellFacade.TILE_PEOPLE_GET_PUBLIC_PHOTOS:
						sendNotification(ShellFacade.SLIDE_PEOPLE_GET_PUBLIC_PHOTOS_SUCCESS, cl);
						break;
					case ShellFacade.TILE_FAVORITES_GET_LIST:
						sendNotification(ShellFacade.SLIDE_FAVORITES_GET_LIST_SUCCESS, cl);
						break;
				}
			}
		}
		
		private function batchDownload(e:Event):void {
			var lp:ListPhotosProxy = facade.retrieveProxy(ListPhotosProxy.NAME) as ListPhotosProxy;
			var pl:PagedPhotoList = lp.currentList.photos;
			if(pl) {
				sendNotification(ShellFacade.PHOTO_BATCH_DOWNLOAD, pl);
			}
		}
		
		// Getters
		private function get pagePN():PreviousAndNextHB {
			return cb.pagePN as PreviousAndNextHB;
		}
		
		private function get cb():ThumbsCB {
			return viewComponent as ThumbsCB;
		}
	}
}